package com.elink.enginerring.video;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.Map;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.elink.enginerring.common.Constant;
import com.elink.enginerring.common.ErrorType;
import com.elink.enginerring.datasource.DataSourceContextHolder;
import com.elink.enginerring.datasource.DataSourceType;
import com.elink.enginerring.label.service.ILabelService;
import com.elink.enginerring.user.entity.User;
import com.elink.enginerring.util.JSONUtil;
import com.elink.enginerring.video.co.CourseCo;
import com.elink.enginerring.video.entity.Chapter;
import com.elink.enginerring.video.entity.Course;
import com.elink.enginerring.video.service.ICourseService;
import com.wordnik.swagger.annotations.ApiParam;

/**
 * 课程
 * @author hxjxiaojun
 * @time   2016年10月31日
 */
@Controller
@RequestMapping(value="/view/course")
@Api(value="课程接口")
@ResponseBody
public class CourseController {
	@Autowired
	private ICourseService courseService;
	@Autowired
	private ILabelService labelService;

	/**
	 * 获取课程列表
	 * @param co
	 * @return
	 */
	@RequestMapping(value="/getList",method=RequestMethod.GET)
	@ApiOperation(value="获取课程列表", httpMethod = "GET",  notes = "分页获取课程列表")
	public Map<String, Object>getList(@ModelAttribute @Valid CourseCo co){
		DataSourceContextHolder.setDbType(DataSourceType.DBVIDEO);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType()); 
		return courseService.getList(co);
	}
	
	/**
	 * 获取章节
	 * @param parentId
	 * @return
	 */
	@RequestMapping(value="/getChapter",method=RequestMethod.GET)
	@ApiOperation(value="获取章节", httpMethod = "GET",  notes = "获取章节")
	public Map<String, Object>getChapter(@RequestParam(value="courseId",required=true)int courseId){
		DataSourceContextHolder.setDbType(DataSourceType.DBVIDEO);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		return courseService.getChapter(courseId);
	}
	/**
	 * 添加章节
	 * @param chapter
	 * @return
	 */
	@RequestMapping(value="/addChapter",method=RequestMethod.POST)
	@ApiOperation(value="添加章节", httpMethod = "POST",  notes = "添加章节")
	public Map<String, Object> addChapter(@ModelAttribute  @Valid Chapter chapter){
		DataSourceContextHolder.setDbType(DataSourceType.DBVIDEO);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		return courseService.addChapter(chapter);
	}
	/**
	 * 修改章节
	 * @param chapter
	 * @return
	 */
	@RequestMapping(value="/updateChapter",method=RequestMethod.POST)
	@ApiOperation(value="修改章节", httpMethod = "POST",  notes = "修改章节")
	public Map<String, Object> updateChapter(@ModelAttribute  @Valid Chapter chapter){
		DataSourceContextHolder.setDbType(DataSourceType.DBVIDEO);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		return courseService.updateChapter(chapter);
	}
	
	/**
	 * 删除章节
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/delChapter",method=RequestMethod.GET)
	@ApiOperation(value="删除章节", httpMethod = "GET",  notes = "删除章节")
	public Map<String, Object>delChapter(@RequestParam(value="chapterId",required=true) int chapterId ){
		DataSourceContextHolder.setDbType(DataSourceType.DBVIDEO);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		return courseService .delChapter(chapterId);
	}
	/**
	 * 添加课程
	 * @param course
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ApiOperation(value="添加课程",httpMethod="POST",notes="添加课程")
	@ResponseBody
	public Map<String,Object> add(@ModelAttribute @Valid Course course,HttpSession session){
		DataSourceContextHolder.setDbType(DataSourceType.DBVIDEO);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		User user = 	(User) session.getAttribute(Constant.su_in_session);
		course.setCreateUserId(user.getUserId());
		return JSONUtil.getJSON(ErrorType.SUCCESS,courseService.add(course));
	}
	/**
	 * 批量删除课程
	 * @param courseIds
	 * @return
	 */
	@RequestMapping(value="/delCourse",method=RequestMethod.GET)
	@ApiOperation(value="删除课程", httpMethod = "GET",  notes = "删除课程")
	public Map<String, Object> delCourse(
			@ApiParam(value = "课程逐主键ds集合，以逗号进行分割") @RequestParam("courseIds") String courseIds) {
		DataSourceContextHolder.setDbType(DataSourceType.DBVIDEO);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		return courseService.delCourse(courseIds);
	}
	
	/**
	 * 批量添加标签
	 * @return
	 */
	@RequestMapping(value = "/addBatch", method = RequestMethod.POST)
	@ApiOperation(value="批量添加标签",httpMethod="POST",notes="批量添加标签")
	@ResponseBody
	public Map<String, Object>addBatch(@ApiParam(value="标签集合，以逗号进行分割")
	@RequestParam(value="tags",required=true)String tags,HttpSession session){
		DataSourceContextHolder.setDbType(DataSourceType.DBVIDEO);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		User user=(User) session.getAttribute(Constant.su_in_session);
		return JSONUtil.getJSON(ErrorType.SUCCESS,labelService.addBatch(tags,user.getUserId()));
	}
	
	/**
	 * 批量修改标签
	 * @param tags
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/updateBatch", method = RequestMethod.POST)
	@ApiOperation(value="批量修改标签",httpMethod="POST",notes="批量修改标签")
	@ResponseBody
	public Map<String, Object>updateBatch(@ApiParam(value="标签集合，以逗号进行分割")
	@RequestParam(value="tags",required=true)String tags,@ApiParam(value="旧的标签主键集合")
	@RequestParam(value="oldTagId",required=true)String oldTagId,HttpSession session){
		DataSourceContextHolder.setDbType(DataSourceType.DBVIDEO);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		User user=(User) session.getAttribute(Constant.su_in_session);
		return JSONUtil.getJSON(ErrorType.SUCCESS,labelService.updateBatch(tags,oldTagId,user.getUserId()));
	}
	
}
